package com.zscat.mallplus.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jpay.weixin.api.WxPayApi;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.*;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.enums.RoomStatus;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgRechargeOrder;
import com.zscat.mallplus.qsg.QsgRoom;
import com.zscat.mallplus.qsg.QsgStore;
import com.zscat.mallplus.sys.mapper.SysAppletSetMapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.awt.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class SharingTask extends QuartzJobBean {

    @Autowired
    private IQsgRechargeOrderService rechargeOrderService;
    @Autowired
    private WxMsgService wxMsgService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IDeviceService deviceService;
    @Resource
    private SysAppletSetMapper appletSetMapper;
    @Autowired
    private IQsgStoreService storeService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("is_sharing",1);
        qw.ne("order_status",1);
        List<QsgRechargeOrder> orderInfos = rechargeOrderService.list(qw);
        if (null!=orderInfos&&orderInfos.size()>0){
            for (QsgRechargeOrder orderInfo : orderInfos){
                try {
                    QsgStore store = storeService.getById(orderInfo.getStoreId());
                    //待分账
                    Map<String, String> parame = new TreeMap<>();
                    parame.put("mch_id", store.getServerMchId());//服务商
                    parame.put("appid", "wx697f0b89354ff12e");//服务商appid
                    parame.put("sub_mch_id", store.getMchId());//出资方 根据订单的storeId获取
                    parame.put("transaction_id", orderInfo.getWxOrderSn());//微信订单号 根据订单获取
                    parame.put("nonce_str", System.currentTimeMillis() + "");
                    parame.put("out_order_no",orderInfo.getOrderSn());
                    JSONObject jsonObject = new JSONObject();//分账的收款方信息
                    jsonObject.put("type","MERCHANT_ID");
                    jsonObject.put("account","1654635837");
                    jsonObject.put("amount",orderInfo.getPrice().multiply(new BigDecimal(100).multiply(new BigDecimal(store.getScale()))).setScale(0,BigDecimal.ROUND_DOWN));
                    jsonObject.put("description",store.getName()+"_"+orderInfo.getOrderSn());//店铺名+订单号
                    JSONArray jsonArray = new JSONArray();
                    jsonArray.add(jsonObject);
                    parame.put("receivers",jsonArray.toJSONString());
                    String sign = WechatUtil.arrayHmacsha(parame,"a0a4e42ee0f0ef84b71294c7235ca1e2");
                    parame.put("sign",sign);
                    //上线后需改为线上模式
                    String i = WxPayApi.profitsharing(parame, store.getServerCertPath(), store.getServerMchId());//参数 证书地址 服务商证书密码默认即商户号
                    Map<String, Object> resultUn = XmlUtil.xmlStrToMap(i);
                    System.out.println("分账定时任务:"+i);
                    String return_code = MapUtils.getString("return_code", resultUn);
                    String result_code = MapUtils.getString("result_code", resultUn);
                    if (result_code.equalsIgnoreCase("SUCCESS")&&return_code.equalsIgnoreCase("SUCCESS")){
                        orderInfo.setIsSharing(2);
                        rechargeOrderService.updateById(orderInfo);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
